package com.leetcode.LC;

public class LC38 {
    public String countAndSay(int n) {
        n=n-1;
        String a = "1", b = "";
        int start, end;
        for (int i = 0; i < n; i++) {
            start = 0;
            end = 0;
            while (end < a.length()) {
                if (a.charAt(start) == a.charAt(end)) {
                    end++;
                } else {
                    b += (end-start)==0?1:(end-start) + "" + a.charAt(start) + "";
                    start = end;
                }
                if (end == a.length()) {
                    b += (end-start)==0?1:(end-start) + "" + a.charAt(start) + "";
                }
            }
            //System.out.println("a="+a + "\n"+"b=" + b);
            a = b;
            b = "";
        }

        return a;
    }

    public static void main(String[] args) {
        System.out.println(new LC38().countAndSay(2));
    }
}
